package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author gsy
 * @date 2021年09月02日20:26
 * Description
 */
@Controller
public class IndexController {

    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private TemplateEngine templateEngine;//模板引擎，生成Html页面

    //访问首页
    @RequestMapping("/")
    public String getIndex(Model model){
        //1.查询数据
        List category1List = getData();
        //2.数据回显
        model.addAttribute("list",category1List);
       return "index/index";

    }
    //生成首页
    @ResponseBody
    @RequestMapping("/createHtml")
    public Result createHtml(){
        //1.查询数据
        List category1List = getData();
        Context context = new Context();
        context.setVariable("list",category1List);
        //2.准备模板 index/index
        //3.生成页面到指定文件夹下
       Writer printWriter = null;

        try {
            printWriter = new PrintWriter("D:\\temp\\index.html","UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        templateEngine.process("index/index",context,printWriter);
        return Result.ok();
    }
    //获取分类数据
    public List getData(){
        List<BaseCategoryView> baseCategoryViewList = productFeignClient.getBaseCategoryView();
        Map<Long, List<BaseCategoryView>> mapGroupByCategory1Id = baseCategoryViewList.stream().collect(Collectors.groupingBy(baseCategoryView ->
                baseCategoryView.getCategory1Id()
        ));
        List Category1List = new ArrayList<>();
        //一级分类信息
        for (Map.Entry<Long, List<BaseCategoryView>> entry1 : mapGroupByCategory1Id.entrySet()) {
            int i =1;

            Map category1Map = new HashMap();
            category1Map.put("index",i++);
            //二级分类信息
            List categoryChild = new ArrayList();
            Map<Long, List<BaseCategoryView>> mapGroupByCategory2Id = entry1.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            for (Map.Entry<Long, List<BaseCategoryView>> entry2 : mapGroupByCategory2Id.entrySet()) {
                Map category2Map = new HashMap();
                //三级分类信息
                List categoryChild2 = new ArrayList();
                for (BaseCategoryView baseCategoryView : entry2.getValue()) {
                    Map category3Map = new HashMap();
                    category3Map.put("categoryName",baseCategoryView.getCategory3Name());
                    category3Map.put("categoryId",baseCategoryView.getCategory3Id());
                    categoryChild2.add(category3Map);
                }
                category2Map.put("categoryChild",categoryChild2);
                category2Map.put("categoryName",entry2.getValue().get(0).getCategory2Name());
                category2Map.put("categoryId",entry2.getValue().get(0).getCategory2Id());
                categoryChild.add(category2Map);
            }
            category1Map.put("categoryChild",categoryChild);
            category1Map.put("categoryName",entry1.getValue().get(0).getCategory1Name());
            category1Map.put("categoryId",entry1.getValue().get(0).getCategory1Id());
            Category1List.add(category1Map);
        }
        return Category1List;
    }
}
